<template>
	<view class="add-view">
		<view class="top-bar"><uni-navbar :fixed="true" :back="true" color="#fff" :title="title"></uni-navbar></view>

		<view class="form-box">
			<u-form :model="form" ref="uForm">
				<u-form-item class="form-item" required label="标题" prop="title">
					<!-- block -->
					<u-input v-model="form.title" type="text" placeholder="请输入标题" />
				</u-form-item>

				<u-form-item class="form-item" required label="分类" prop="type">
					<u-input v-model="articleType" type="select" :select-open="articleTypeShow" placeholder="请选择分类" @click="articleTypeShow = true" />
					<u-select :list="articleTypeList" v-model="articleTypeShow" @confirm="articleTypeConfirm"></u-select>
				</u-form-item>

				<u-form-item class="form-item" label="封面图" label-position="top">
					<!-- block -->
					<uni-file-upload v-model:list="fileList" :type="['image']" :limit="1"></uni-file-upload>
				</u-form-item>

				<u-form-item class="form-item border-none" required label=" " label-position="top" prop="content">
					<!-- block -->
					<u-input input-align="left" v-model="form.content" type="textarea" placeholder="请输入正文内容" :clearable="false" :height="160" :maxlength="-1" />
				</u-form-item>

				<u-form-item class="form-item" label=" " label-position="top">
					<!-- block -->
					<uni-file-upload v-model:list="fileList2" :type="['all', 'image']" :limit="10"></uni-file-upload>
				</u-form-item>
			</u-form>
		</view>

		<view class="fiexd-btm"><u-button class="form-btn" type="error" @click="confirm">提交</u-button></view>
	</view>
</template>

<script>
import dateTimePicker from '@/components/uni-datetime-picker';
import uniFileUpload from '@/components/uni-uploader.vue';
import { mapState } from 'vuex';
export default {
	components: {
		dateTimePicker,
		uniFileUpload
	},
	data() {
		return {
			id: '',
			navItem: 0,
			title: '图文添加',

			fileList: [],
			fileList2: [],

			articleType: '',
			articleTypeShow: false,
			articleTypeList: [],

			endTime: '',

			form: {
				title: '',
				type: '1',
				secType: '2',
				articleType: '',
				content: ''
			},
			rules: {
				title: [
					{
						required: true,
						message: '请输入标题',
						trigger: 'change'
					}
				],
				articleType: [
					{
						required: true,
						message: '请输入分类',
						trigger: 'change'
					}
				],
				content: [
					{
						required: true,
						message: '请输入正文内容',
						trigger: 'change'
					}
				]
			}
		};
	},
	onLoad(option) {
		this.loadLogin();
		let p1 = this.loadDict('xxpx', 'articleTypeList');

		Promise.all([p1]).then(() => {
			if (option && option.id) {
				this.id = option.id;
				this.title = this.title.replace('添加', '修改');
				uni.setNavigationBarTitle({
					title: this.title
				});
				console.log('loadData');
				this.loadData();
			}
		});
	},
	onReady() {
		this.$refs.uForm.setRules(this.rules);
	},
	computed: {
		...mapState(['userInfo'])
	},
	methods: {
		articleTypeConfirm(e) {
			this.articleType = e[0].label;
			this.form.articleType = e[0].value;
		},
		timeChange({ date, dt }, idx) {
			this.form[idx] = date.year + '-' + date.month2 + '-' + date.day2;
		},
		loadLogin() {
			if (this.userInfo.Authorization) {
				this.clientCode = this.userInfo.sysUser.clientCode;
				this.form.clientCode = this.userInfo.sysUser.clientCode;
				this.form.createBy = this.userInfo.sysUser.id;
			} else {
				this.$logout();
			}
		},
		loadData() {
			this.$request('/dushuxuexi/article/query/' + this.id).then((res) => {
				this.form = res.data.dsxxArticle;
				let regx = /<[^>]*>|<\/[^>]*>/gm;
				if (this.form.content) {
					this.form.content = this.form.content.replace(regx, '');
				}
				this.secTypeList.forEach((item) => {
					if (res.data.dsxxArticle.secType == item.value) {
						this.secType = item.label;
						this.form.secType = item.value;
					}
				});
				this.fileList = res.data.fileList;
				this.fileList2 = res.data.fileList2;
			});
		},
		loadDict(type, list) {
			return new Promise((resole, reject) => {
				this.$request('/system/sys-dict/dictList', {
					clientCode: this.clientCode,
					sysDict: {
						dictType: type
					}
				}).then((res) => {
					console.log('loadDict');
					resole('loadDict');
					this[list] = [];
					res.data &&
						res.data.forEach((item) => {
							this[list].push({
								value: item.dictValue,
								label: item.dictName
							});
						});
				});
			});
		},
		timeChange({ date, dt }, idx) {
			this.form[idx] = date.year + '-' + date.month2 + '-' + date.day2 + ' ' + date.hour2 + ':' + date.minute2 + ':' + '00';
			this[idx] = Math.round(dt / 1000);
		},
		confirm() {
			this.$refs.uForm.validate((valid) => {
				if (!valid) {
					return false;
				} else {
					let data = {};
					if (this.id) {
						this.form.id = this.id;
					}
					data = {
						userId: this.userId,
						clientCode: this.clientCode,
						fileList: this.fileList,
						fileList2: this.fileList2,
						dsxxArticle: {
							createBy: this.userId,
							clientCode: this.clientCode,
							...this.form
						}
					};
					this.$request('/dushuxuexi/article/add', data, {
						load: true
					}).then((res) => {
						this.$msg(res.msg);
						setTimeout(() => {
							let pre = this.$prePage();
							if (pre && pre.loadData) {
								pre.loadData('refresh');
							}
							uni.navigateBack();
						}, 500);
					});
				}
			});
		}
	}
};
</script>

<style lang="scss">
.add-view {
	background-color: #fff;
}

.form-box {
	border-top: 20rpx solid $u-border-color;

	.form-item {
		@include mp-flex;
		border-bottom: 20rpx solid $u-border-color;

		&.border-none {
			border-bottom: 0;
		}
	}
}

.fiexd-btm {
	position: static;
	padding: 30rpx;
}
</style>
